package com.tejava.cafe_in;

import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.view.Window;

public class IntroActivity extends Activity{
	Handler h;
	ConnectivityManager mManager;
	NetworkInfo mobile;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.intro);
		h = new Handler();
		h.postDelayed(irun, 2000);
		
		mManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
		mobile = mManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		
		if(!mobile.isConnected()){
			try {
				TelephonyAidl(this, true);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	Runnable irun = new Runnable(){
		public void run(){
			Intent i = new Intent(IntroActivity.this, CustomTabActivity.class);
			startActivity(i);
			finish();
			
			overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
		}
	};
	
	@TargetApi(5)
	@Override
	public void onBackPressed(){
		super.onBackPressed();
		h.removeCallbacks(irun);
	}
	
	public void TelephonyAidl(Context context, boolean dataconnectivity) throws Exception {
		TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		
		@SuppressWarnings("rawtypes")
		Class c = Class.forName(tm.getClass().getName());
		Method m = c.getDeclaredMethod("getITelephony");
		m.setAccessible(true);
		ITelephony telephonyService;
		telephonyService = (ITelephony) m.invoke(tm);

		if (dataconnectivity) {
			telephonyService.enableDataConnectivity();
			telephonyService.enableLocationUpdates();
			telephonyService.updateServiceLocation();
		} else {
			telephonyService.disableDataConnectivity();
		}
	}
	
}
